`:top
`!Java Native Access`! (`!JNA`!) is a community-developed library that provides `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(software_platform)]`_`f programs easy access to `F33f`_`[native shared libraries`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Shared_library]`_`f without using the `F33f`_`[Java Native Interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Native_Interface]`_`f (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no `F33f`_`[boilerplate`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Boilerplate_code]`_`f or generated `F33f`_`[glue code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Glue_code]`_`f is required.
Since Java 22, the `F33f`_`[Foreign Function and Memory API`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Native_Interface]`_`f was provided as a standard modern alternative.
>>Contents
• `F0af`_`[Architecture`#architecture]`_`f
• `F0af`_`[Mapping types`#mapping-types]`_`f
• `F0af`_`[Memory byte alignment for data structures`#memory-byte-alignment-for-data-structures]`_`f
• `F0af`_`[Example`#example]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>Architecture
The JNA library uses a small native library called `F33f`_`[foreign function interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Foreign_function_interface]`_`f library (`F33f`_`[libffi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Libffi]`_`f) to dynamically invoke `F33f`_`[native code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Native_code]`_`f. The JNA library uses native functions allowing code to load a library by name and retrieve a `F33f`_`[pointer`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pointer_(computer_programming)]`_`f to a function within that library, and uses `F33f`_`[libffi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Libffi]`_`f library to invoke it, all without `F33f`_`[static bindings`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_binding]`_`f, `F33f`_`[header files`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Header_files]`_`f, or any compile phase. The developer uses a `F33f`_`[Java interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_interface]`_`f to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high development overhead of configuring and building `F33f`_`[JNI`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JNI]`_`f code.
JNA is built and tested on `F33f`_`[macOS`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=MacOS]`_`f, `F33f`_`[Microsoft Windows`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_Windows]`_`f, `F33f`_`[FreeBSD`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=FreeBSD]`_`f / `F33f`_`[OpenBSD`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=OpenBSD]`_`f, `F33f`_`[Solaris`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Solaris_(operating_system)]`_`f, `F33f`_`[Linux`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Linux]`_`f, `F33f`_`[AIX`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=AIX]`_`f, `F33f`_`[Windows Mobile`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Windows_Mobile]`_`f, and `F33f`_`[Android`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Android_(operating_system)]`_`f. It is also possible to tweak and recompile the native build configurations to make it work on most other platforms that run Java.
>>Mapping types
The following table shows an overview of types mapping between Java and native code and supported by the JNA library.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]
`t
| Native Type | Size | Java Type | Common Windows Types |
|---|---|---|---|
| char | 8-bit integer | byte | BYTE, TCHAR |
| short | 16-bit integer | short | WORD |
| wchar_t | 16/32-bit character | char | TCHAR |
| int | 32-bit integer | int | DWORD |
| int | boolean value | boolean | BOOL |
| long | 32/64-bit integer | NativeLong | LONG |
| long long | 64-bit integer | long | __int64 |
| float | 32-bit FP | float | |
| double | 64-bit FP | double | |
| char * | C string | String | LPCSTR |
| void* | pointer | Pointer | LPVOID, HANDLE, LPXXX |
`t
`!Note:`! The meaning of TCHAR changes between char and wchar_t according to some preprocessor definitions. LPCTSTR follows.
>>Memory byte alignment for data structures
Native libraries have no standardized memory byte alignment flavor. JNA defaults to an OS platform specific setting, that can be overridden by a library specific custom alignment. If the alignment details are not given in the documentation of the native library, the correct alignment must be determined by trial and error during implementation of the Java wrapper.
>>Example
The following program loads the local `F33f`_`[C standard library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_standard_library]`_`f implementation and uses it to call the `*`F33f`_`[printf`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Printf]`_`f`* function.
`!Note:`! The following code is portable and works the same on `F33f`_`[Windows`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_Windows]`_`f and POSIX (`F33f`_`[Linux`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Linux]`_`f / `F33f`_`[Unix`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Unix]`_`f / `F33f`_`[macOS`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=MacOS]`_`f) platforms.
`B100`F9d9import com.sun.jna.Library;`f`b
`B100`F9d9import com.sun.jna.Native;`f`b
`B100`F9d9import com.sun.jna.Platform;`f`b
`B100`F9d9`f`b
`B100`F9d9/** Simple example of native library declaration and usage. */`f`b
`B100`F9d9public class HelloWorld {`f`b
`B100`F9d9 public interface CLibrary extends Library {`f`b
`B100`F9d9 CLibrary INSTANCE = (CLibrary) Native.loadLibrary(`f`b
`B100`F9d9 (Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);`f`b
`B100`F9d9 void printf(String format, Object... args);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public static void main(String[] args) {`f`b
`B100`F9d9 CLibrary.INSTANCE.printf("Hello, World\\n");`f`b
`B100`F9d9 for (int i = 0; i < args.length; i++) {`f`b
`B100`F9d9 CLibrary.INSTANCE.printf("Argument %d: %s\\n", i, args[i]);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
The following program loads the `F33f`_`[C POSIX library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_POSIX_library]`_`f and uses it to call the standard `*`F33f`_`[mkdir`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mkdir]`_`f`* function.
`!Note:`! The following code is portable and works the same on `F33f`_`[POSIX`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=POSIX]`_`f standards platforms.
`B100`F9d9import com.sun.jna.Library;`f`b
`B100`F9d9import com.sun.jna.Native;`f`b
`B100`F9d9`f`b
`B100`F9d9/** Simple example of native C POSIX library declaration and usage. */`f`b
`B100`F9d9public class ExampleOfPOSIX {`f`b
`B100`F9d9 public interface POSIX extends Library {`f`b
`B100`F9d9 public int chmod(String filename, int mode);`f`b
`B100`F9d9 public int chown(String filename, int user, int group);`f`b
`B100`F9d9 public int rename(String oldpath, String newpath);`f`b
`B100`F9d9 public int kill(int pid, int signal);`f`b
`B100`F9d9 public int link(String oldpath, String newpath);`f`b
`B100`F9d9 public int mkdir(String path, int mode);`f`b
`B100`F9d9 public int rmdir(String path);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public static void main(String[] args) {`f`b
`B100`F9d9 // It is possible to load msvcrt for its partial POSIX support on Windows...`f`b
`B100`F9d9 POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class);`f`b
`B100`F9d9 // but it will still fail on Windows due to /tmp being missing.`f`b
`B100`F9d9 posix.mkdir("/tmp/newdir", 0777);`f`b
`B100`F9d9 posix.rename("/tmp/newdir","/tmp/renamedir");`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
The program below loads the `!`F33f`_`[Kernel32.dll`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Kernel32.dll]`_`f`! and uses it to call the `*`F33f`_`[Beep`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Beep_(sound)]`_`f`* and `*`F33f`_`[Sleep`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Sleep_(system_call)]`_`f`* functions.
`!Note:`! The following code works only on `F33f`_`[Windows`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_Windows]`_`f platforms.
`B100`F9d9import com.sun.jna.Library;`f`b
`B100`F9d9import com.sun.jna.Native;`f`b
`B100`F9d9`f`b
`B100`F9d9/** Simple example of Windows native library declaration and usage. */`f`b
`B100`F9d9public class BeepExample {`f`b
`B100`F9d9 public interface Kernel32 extends Library {`f`b
`B100`F9d9 // FREQUENCY is expressed in hertz and ranges from 37 to 32767`f`b
`B100`F9d9 // DURATION is expressed in milliseconds`f`b
`B100`F9d9 public boolean Beep(int FREQUENCY, int DURATION);`f`b
`B100`F9d9 public void Sleep(int DURATION);`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public static void main(String[] args) {`f`b
`B100`F9d9 Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);`f`b
`B100`F9d9 lib.Beep(698, 500);`f`b
`B100`F9d9 lib.Sleep(500);`f`b
`B100`F9d9 lib.Beep(698, 500);`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
>>See also
• `F33f`_`[JNAerator`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JNAerator]`_`f
• `F33f`_`[P/Invoke`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Platform_Invocation_Services]`_`f
• `F33f`_`[SWIG`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SWIG]`_`f
>>References
`:cite-note-5-17-0-1`!1.`! "Release 5.17.0". `*`F33f`_`[GitHub`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GitHub]`_`f`*. 2025-03-16.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Default Type Mappings". jna.dev.java.net. Retrieved 2011-08-02.
>>External links
• Java Native Access Web Page
• Java Native Access - Download page
• Java Native Access - User Mailing List
• `:citereffriesen2008`aFriesen, Jeff (5 February 2008). "Open source Java projects: Java Native Access". Open Source Java Tutorials. `*`F33f`_`[JavaWorld`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaWorld]`_`f`*. Retrieved 2020-07-27.
• `:citerefmorris2009`aMorris, Stephen B. (20 May 2009). "Protect Your Legacy Code Investment with JNA". `*today.java.net`*. Archived from the original on 2015-01-13.
• `:citerefdasgupta2009`aDasgupta, Sanjay (11 November 2009). "Simplify Native Code Access with JNA". `*today.java.net`*. Archived from the original on 2009-11-15.
• `:citerefdoubrovkine2011`aDoubrovkine, Daniel (20 June 2011). "JNA is now a Githubber". `*code.dblock.org`*. Retrieved 2020-07-27.
• `:citerefkiaer2010`aKiaer, Jesper (21 March 2010). "Calling the Lotus Domino C-API with JNA". `*Nevermind.dk`*. Retrieved 2020-07-27.
`c`F0af`_`[↑ Back to top`#top]`_`f`a